package DTO;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.util.ArrayList;

import org.Consts;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

import GameTools.ydAnchor;
import GameTools.ydPoint;
import GameTools.ydTransformation;

public class PartDTO implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5806905342421771661L;
	int PK;
	public ydPoint OriginalSize;
	public ydPoint Position;
	public PartDTO Parent;
	public ArrayList<PartDTO> Childern;
	public ArrayList<PartDTO> ChildernTop;
	public ArrayList<PartDTO> ChildernBottom;
	public Bitmap image;
	public ydAnchor MainAnchor;
	
	public Boolean update=false;

	// public ydTransformation Transformations;

	public PartDTO() {
		OriginalSize = new ydPoint();
		Position = new ydPoint(); 
		Childern = new ArrayList<PartDTO>();
		ChildernTop = new ArrayList<PartDTO>();
		ChildernBottom = new ArrayList<PartDTO>();
		MainAnchor = new ydAnchor();
		// Transformations=new ydTransformation();
	}

	public void recDraw(Canvas canvas, Paint ppp) {
		for (PartDTO p : ChildernBottom) {
			p.recDraw(canvas, ppp);
		}
		if (Parent == null)
			canvas.drawBitmap(image, Position.x, Position.y, ppp);
		else
			canvas.drawBitmap(image, Position.x,
					Position.y, ppp);
		for (PartDTO p : ChildernTop) {
			p.recDraw(canvas, ppp);
		}
	}

	public void MoveTo(float x, float y) {
		//Position.setCoordinates(x, y);
		//recMoveTo(x-Position.x, y-Position.y);
		delta=new ydPoint(x-Position.x-MainAnchor.Position.x, y-Position.y-MainAnchor.Position.y);
		update=true;
	}
	public ydPoint delta= new ydPoint();
	public void recMoveTo(float x, float y) {
		if(Parent==null)
			Position.setCoordinates(Position.x+x, Position.y+y);
		else
			Position.setCoordinates(x+Position.x, y+Position.y);
		
		for(PartDTO p: ChildernTop){
			p.recMoveTo(x, y);
		}
		for(PartDTO p: ChildernBottom){
			p.recMoveTo(x, y);
		}
	}

	public void recUpdate() {

		//	if(update){
				recMoveTo(delta.x, delta.y);
				update=false;
			//}

		
		for(PartDTO p: Childern){
			p.recUpdate(delta);
		}
		
		delta.setCoordinates(0, 0);
	}
	
	public void recUpdate(ydPoint d) {

		delta.setCoordinates(d);
		//	if(update){
				recMoveTo(delta.x, delta.y);
				update=false;
			//}

		
		for(PartDTO p: Childern){
			p.recUpdate(delta);
		}
		
		delta.setCoordinates(0, 0);
	}

}
